use strict;

use lib '/var/www/perl/galleri';

use Galleri;
my $g = new Galleri();
my $pjx = $g->pjx;

$g->add_css
(q{
#users th,td {
 border: 1px solid black;
 padding: 2px;
}
#users input {
 border: 1px solid #e0e0e0;
}
#users button {
 border: 1px solid #d0d0d0;
 width: 100%;
}
#users input {
 width: 100%;
}

});

  $g->add_js
    (q{

function nudge1(img) {
  img.style.left = '1px';
  img.style.top = '1px';
}

function nudge2(img) {
  img.style.left = '0px';
  img.style.top = '0px';
}

function album_is_modified(id){
  d=document.getElementById("apply" + id);
  if(!d)
    return;
  d.setAttribute("class", "active");
  d.src="images/stock-apply.png";
  
}
function handle_create(id){
  
  if(!id)
    return;
  
  table = document.getElementById("albums");
  if(!table)
    returh;
  
  table.insertRow( table.rows.length );
  
  tr = table.rows[ table.rows.length-1 ];
  tr.id = "albumrow" + id;  
  
  
  
  inp = document.createElement("input");
  inp.type = "hidden";
  inp.id = "temp";
  inp.value = id;
  
  tr.appendChild(inp);
  
  jsLoadAlbumRow( ["temp"], ["albumrow"+id] );
  
  tr.removeChild(inp);
  
  document.getElementById("new_name").value = "";
  document.getElementById("new_level").value = "1";

}
function handle_toggle(id,connected) {
  
  image = document.getElementById("connect" + id);

  if(!image)
    return;
  
  if(connected==1)
    image.src="images/stock-connect.png";
  else
    image.src="images/stock-disconnect.png";
}
function handle_modified(id,unchanged){
  
  image = document.getElementById("apply" + id);

  if(!image)
    return;
  
  if(unchanged==1){
    image.src="images/stock-apply-disabled.png";
    image.setAttribute("class","inactive");
    image.setAttribute("onclick", "" );
    image.setAttribute("onmousedown", "" );
    image.setAttribute("onmouseup", "" );
  }
  else{
    image.src="images/stock-apply.png";
    image.setAttribute("class","active");
    image.setAttribute("onclick", "jsStoreChanges(['albumname" + id + "','albumlevel" + id + "','albumid" + id + "'],['albumrow" + id + "'])" );
    image.setAttribute("onmousedown", "nudge1(this); return true;" );
    image.setAttribute("onmouseup", "nudge2(this); return true;" );

  }
  
}
function handle_delete(id){
  
  tr = document.getElementById("albumrow" + id);
  
  if(!tr)
    return;
  
  tr.parentNode.removeChild(tr);
  
}
});


my $html = sprintf q{<table id="users"><tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th></tr>},
  "Brukernavn", "Emailadresse", "Level", "Passord", "Siste aktivitet", "Handling";

for my $user (Galleri::DB->resultset('Users')->all){
  
  $html .= sprintf
    q{<input type="hidden" id="user%d" value="%d">
      <tr><td>%s</td><td>%s</td><td>%d</td><td>%s</td><td>%s</td><td>
      
<img id="apply%d" class="inactive" src="images/stock-apply-disabled.png"
     onmousedown="nudge1(this); return true;" onmouseup="nudge2(this); return true;"
     onclick="jsStoreChanges(['username%d','userlevel%d','userid%d'],['userrow%d'])">
<img id="delete%d" class="active" src="images/stock-cancel.png"
     onmousedown="nudge1(this); return true;" onmouseup="nudge2(this); return true;"
     onclick="jsDeleteUser(['userid%d'],[handle_delete])">

</td></tr>},
  ($user->id)x2,$user->username, $user->email, $user->accesslevel, "***", "", 
    ($user->id)x7;
  
}

# <img id="edit%d" class="active" src="images/stock-book.png"
#      onmousedown="nudge1(this); return true;" onmouseup="nudge2(this); return true;"
#      onclick="window.location='user_admin.pl?user_id=%d'">

$html .= "</table>";

$g->add_content( subject => "Medlemsoversikt",
		 body => [$html] );

$g->output;
